#include "ChargingModel.h"
#include "Common/DataStructureMacro.h"
#include "ssas_api.h"
#define LOG_TAG "HMI"
#include "log_api.h"

typedef enum 
{
    IDLE,
    DISCHARGING,
    CHARFING,
} MachineState;

static MachineState currentStatus;
static uint16_t rawcChrgingSts;     //充电线连接状态
static uint16_t rawChrgStsIndOn;    //充电状态
static Option_U8 showChrgingSts;
static Option_U8 showChrgStsIndOn;
static uint8_t      widget_visible_mask = 0;

static void init()
{
    currentStatus = IDLE;
    rawcChrgingSts = 0;
    rawChrgStsIndOn = 0;
    showChrgingSts.value_ = 0;
    showChrgingSts.update_ = 0;
    showChrgStsIndOn.value_ = 0;
    showChrgStsIndOn.update_ = 0;
}

static MachineState getNewState()
{
    MachineState newState = IDLE;
    int msgState = COM_MSG_INIT;
    Std_ReturnType comRet = E_NOT_OK;
    do
    {
        msgState = get_message_state(COM_IC_HEV_GENERAL_STATUS_1);//todo 可能不是这个报文
        if(COM_MSG_INIT == msgState || COM_MSG_TIMEOUT == msgState)
        {
            newState = IDLE;
            break;
        }
        
        comRet = Com_ReceiveSignal(COM_SID_DrvSysRdyIndctn, &rawcChrgingSts);
        if(E_NOT_OK == comRet)
        {
            newState = IDLE;
            break;
        }

        comRet = Com_ReceiveSignal(COM_SID_ChrgdSttIndctn, &rawChrgStsIndOn);
        if(E_NOT_OK == comRet)
        {
            newState = IDLE;
            break;
        }

        if(OFF == rawcChrgingSts)
        {
            newState = IDLE;
        }
        else 
        {
            if(OFF == rawChrgStsIndOn)
            {
                newState = DISCHARGING;
            }
            else
            {
                newState = CHARFING;
            }
        }

    } while (false);
    
    return newState;
}

static void handlStateTransition(MachineState newStatus)
{    
    if(currentStatus != newStatus)
    {  
        currentStatus = newStatus;
    }
}

static void postprocess()
{
    switch (currentStatus)
    {
        case IDLE:
        {
            setOptionValue_U8(&showChrgingSts, false);
            setOptionValue_U8(&showChrgStsIndOn, false);
            break;
        }

        case DISCHARGING:
        {
            setOptionValue_U8(&showChrgingSts, true);
            setOptionValue_U8(&showChrgStsIndOn, false);
            break;
        }

        case CHARFING:
        {
            setOptionValue_U8(&showChrgingSts, true);
            setOptionValue_U8(&showChrgStsIndOn, true);
            break;
        }
    
        default:
            LOG_E("unknown status : %d\n", currentStatus);
            break;
    }
}

static void run()
{
    MachineState newStatus = getNewState();
    handlStateTransition(newStatus);
    postprocess(); 
}

static void release()
{

}

ChargingModel g_ChargingModelInstance = {
    // ImeterModel
    {
        init,
        run,
        release,
        MIN_PERIOD,
    },
};

// ----------------------------------View------------------------------
void refresh_tt19View(ItuElement* elememt, bool isUnconditional)
{
    if(showChrgingSts.update_)
    {
        ituCheckBoxSetChecked((ITUCheckBox*)elememt->widget_, showChrgingSts.value_);
        showChrgingSts.update_ = false;
    }
}

void reporting_self_validity_tt19View(ItuElement* elememt)
{
    uint8_t is_visible = (elememt && elememt->isVaild_) ? 0x01 : 0x00;
    widget_visible_mask <<= 1;
    widget_visible_mask |= is_visible;
}

void refresh_tt20View(ItuElement* elememt, bool isUnconditional)
{
    if(showChrgStsIndOn.update_)
    {
        ituCheckBoxSetChecked((ITUCheckBox*)elememt->widget_, showChrgStsIndOn.value_);
        showChrgStsIndOn.update_ = false;
    }
}

void reporting_self_validity_tt20View(ItuElement* elememt)
{

}

void refresh_tt8View(ItuElement* elememt, bool isUnconditional)
{
    
}

void reporting_self_validity_tt8View(ItuElement* elememt)
{

}

void refresh_tt9View(ItuElement* elememt, bool isUnconditional)
{
}

void reporting_self_validity_tt9View(ItuElement* elememt)
{

}